package com.Sdebruijn.curriculumvitae.asynctask;

import java.io.UnsupportedEncodingException;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.db.DatabaseHelper;
import com.Sdebruijn.curriculumvitae.db.DatabaseManager;
import com.Sdebruijn.curriculumvitae.models.Applicant;
import com.Sdebruijn.curriculumvitae.sync.remote.Connectivity;
import com.Sdebruijn.curriculumvitae.sync.remote.RestClient;
import com.Sdebruijn.curriculumvitae.sync.remote.RestClient.RequestMethod;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.google.gson.Gson;
import com.j256.ormlite.dao.Dao;

/**
 * Loads the data of a Person in a separate background thread with
 * {@link AsyncTask}
 * 
 * @author Stefan de Bruijn
 */
// TODO Eventually replace this with a service - per example with RoboSpice
public class DownloadPersonDataAsyncTask extends AsyncTask<Void, Void, Void> {

	private Applicant applicant;

	private boolean isOnline;

	/*
	 * Data fields from the fragment, in order according to the view
	 */
	private LinearLayout ll_person_data;
	private ProgressWheel pg;

	/**
	 * Context used to create a {@link Dao} linked to {@link DatabaseHelper}
	 */
	private Context context;

	private View view;

	public DownloadPersonDataAsyncTask(View view, Context context) {
		this.context = context;
		this.view = view;

		ll_person_data = (LinearLayout) view.findViewById(R.id.ll_person_data);
		pg = (ProgressWheel) view.findViewById(R.id.pw_spinner);
	}

	@Override
	protected void onPreExecute() {
		// Show the progress wheel and make it spin
		ViewUtils.switchVisibleState(ll_person_data, ViewUtils.State.INVISIBLE, pg,
				ViewUtils.State.VISIBLE);
		pg.spin();
	}

	@Override
	protected Void doInBackground(Void... params) {

		if (Connectivity.isConnected(context)) {
			isOnline = true;

			Gson gson = new Gson();
			String response = "";
			try {
				RestClient client = new RestClient(
						"http://dl.dropbox.com/u/37565310/person_only.json");
				client.execute(RequestMethod.GET);
				response = client.getResponse();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			applicant = gson.fromJson(response, Applicant.class);
			DatabaseManager instance = DatabaseManager.getInstance();
			instance.addApplicant(applicant);
			instance.addApps(applicant.apps, applicant);
			instance.addHobbies(applicant.hobbies, applicant);
			instance.addCourses(applicant.courses, applicant);

		} else {
			isOnline = false;
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void results) {
		if (!isOnline) {
			Toast.makeText(context,
					"You're currently offline. Shown information might be outdated.",
					Toast.LENGTH_LONG).show();
		}
		LoadPersonDataAsyncTask mLoadPersonDataAsyncTask = new LoadPersonDataAsyncTask(view,
				context);
		mLoadPersonDataAsyncTask.execute();
	}
}
